package com.itboye.zhujia.myzhujia;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.zhujia.R;
import com.itboye.zhujia.adapter.OrderViewPagerAdapter;
import com.itboye.zhujia.base.BaseActivity;
import com.itboye.zhujia.qianyue.OrderFragment;
/**
 * 我的签约
 */
public class ActivityMySign extends BaseActivity implements OnClickListener {
	TextView add_shap_title_tv;
	ImageView close_icon;
	private ViewPager viewpager;
	private ArrayList<Fragment> fragmentList;
	private TextView tv_guid1, tv_guid2, tv_guid3, tv_guid4, tv_guid5,
			tv_guid6, allorder;
	private ImageView cursor;
	private int currIndex;// 当前页卡编号
	private float offset;// 图片移动的偏移量
	private RelativeLayout backrela;
	private ImageView message;
	private int index;

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		setContentView(R.layout.fragment_qianyue);
		add_shap_title_tv.setText("签约管理");
		close_icon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		index = 0;
//		allorder.setOnClickListener(new OnClickListener() {// 查看全部订单
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				// Intent intent=new
//				// Intent(MyOrderActivity.this,QueryAllOrder.class);
//				// startActivity(intent);
//			}
//		});
		initTextView();
		initImage();
		initViewPager();
	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * @Override protected void onCreate(Bundle savedInstance) { // TODO
	 * Auto-generated method stub super.onCreate(savedInstance);
	 * setContentView(R.layout.activity_myorder);
	 * 
	 * initTextView(); initImage(); initViewPager(); }
	 */

	// 初始化标签
	public void initTextView() {
		tv_guid1.setOnClickListener(new txListener(0));
		tv_guid2.setOnClickListener(new txListener(1));
		tv_guid3.setOnClickListener(new txListener(2));
		tv_guid4.setOnClickListener(new txListener(3));

		tv_guid5.setOnClickListener(new txListener(4));
		tv_guid6.setOnClickListener(new txListener(5));
	}

	public class txListener implements OnClickListener {
		private int index = 0;

		public txListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			viewpager.setCurrentItem(index);
		}
	}

	/*
	 * 初始化图片的位移像素
	 */
	public void initImage() {
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		int screenH = dm.heightPixels;
		offset = screenW / 6.0f;
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				(int) offset, 2);
		cursor.setLayoutParams(params);
	}

	/*
	 * 初始化ViewPager
	 */
	public void initViewPager() {
		fragmentList = new ArrayList<Fragment>();
		Fragment allFragment = new OrderFragment(1);
		Fragment sendFragment = new OrderFragment(3);
		Fragment receiveFragment = new OrderFragment(6);
		Fragment payFragment = new OrderFragment(5);
		Fragment payFragment5 = new OrderFragment(5);
		Fragment payFragment6 = new OrderFragment(5);
		fragmentList.add(allFragment);
		fragmentList.add(sendFragment);
		fragmentList.add(receiveFragment);
		fragmentList.add(payFragment);
		fragmentList.add(payFragment5);
		fragmentList.add(payFragment6);
		// Intent intent = getIntent();
		// int i = intent.getIntExtra("index", 0);
		// currIndex = i;
		//
		// //给ViewPager设置适配器
		viewpager.setAdapter(new OrderViewPagerAdapter(
				getSupportFragmentManager(), fragmentList));

		viewpager.setCurrentItem(0);// 设置当前显示标签页为第一页
		setTextColor(0 + 1);

		tranSlate(0, 0);

		// Animation animation = new TranslateAnimation(currIndex * one, i
		// * one, 0, 0);// 平移动画
		// currIndex = i;
		// animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
		// animation.setDuration(0);// 动画持续时间0.2秒
		// image.startAnimation(animation);// 是用ImageView来显示动画的

		viewpager.setOnPageChangeListener(new MyOnPageChangeListener());// 页面变化时的监听器

	}

	public class MyOnPageChangeListener implements OnPageChangeListener {
		private float one = offset;// 两个相邻页面的偏移量

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub

			// Toast.makeText(App.ctx, ""+arg0, 0).show();
			// Animation animation = new TranslateAnimation(currIndex * one,
			// arg0
			// * one, 0, 0);// 平移动画
			// currIndex = arg0;
			// animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
			// animation.setDuration(200);// 动画持续时间0.2秒
			// image.startAnimation(animation);// 是用ImageView来显示动画的

			tranSlate(arg0, 200);
			currIndex = arg0;
			int i = currIndex + 1;
			setTextColor(i);

			// Toast.makeText(MyOrderActivity.this, "您选择了第"+i+"个页卡",
			// Toast.LENGTH_SHORT).show();
		}
	}

	// 平移动画
	public void tranSlate(int currentItem, long durationMillis) {
		float one = offset;
		Log.v("offest", "" + offset);
		Animation animation = new TranslateAnimation(currIndex * one,
				currentItem * one, 0, 0);// 平移动画
		Log.v("currentoffest", "" + currIndex * one);
		Log.v("currentItem", "" + currentItem * one);
		animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
		animation.setDuration(durationMillis);// 动画持续时间0.2秒
		cursor.startAnimation(animation);// 是用ImageView来显示动画的
	}

	// 设置顶部条目颜色的变化
	public void setTextColor(int i) {
		switch (i) {
		case 1:
			tv_guid1.setTextColor(getResources().getColor(R.color.black_1a));
			tv_guid2.setTextColor(getResources().getColor(R.color.black));
			tv_guid3.setTextColor(getResources().getColor(R.color.black));
			tv_guid4.setTextColor(getResources().getColor(R.color.black));
			tv_guid5.setTextColor(getResources().getColor(R.color.black));
			tv_guid6.setTextColor(getResources().getColor(R.color.black));
			break;
		case 2:
			tv_guid1.setTextColor(getResources().getColor(R.color.black));
			tv_guid2.setTextColor(getResources().getColor(R.color.black_1a));
			tv_guid3.setTextColor(getResources().getColor(R.color.black));
			tv_guid4.setTextColor(getResources().getColor(R.color.black));
			tv_guid5.setTextColor(getResources().getColor(R.color.black));
			tv_guid6.setTextColor(getResources().getColor(R.color.black));
			break;
		case 3:
			tv_guid1.setTextColor(getResources().getColor(R.color.black));
			tv_guid3.setTextColor(getResources().getColor(R.color.black_1a));
			tv_guid2.setTextColor(getResources().getColor(R.color.black));
			tv_guid4.setTextColor(getResources().getColor(R.color.black));
			tv_guid5.setTextColor(getResources().getColor(R.color.black));
			tv_guid6.setTextColor(getResources().getColor(R.color.black));
			break;
		case 4:
			tv_guid1.setTextColor(getResources().getColor(R.color.black));
			tv_guid2.setTextColor(getResources().getColor(R.color.black));
			tv_guid4.setTextColor(getResources().getColor(R.color.black_1a));
			tv_guid3.setTextColor(getResources().getColor(R.color.black));
			tv_guid5.setTextColor(getResources().getColor(R.color.black));
			tv_guid6.setTextColor(getResources().getColor(R.color.black));
			break;
		case 5:
			tv_guid1.setTextColor(getResources().getColor(R.color.black));
			tv_guid2.setTextColor(getResources().getColor(R.color.black));
			tv_guid4.setTextColor(getResources().getColor(R.color.black_1a));
			tv_guid3.setTextColor(getResources().getColor(R.color.black));
			tv_guid5.setTextColor(getResources().getColor(R.color.black));
			tv_guid6.setTextColor(getResources().getColor(R.color.black));
			break;
		case 6:
			tv_guid1.setTextColor(getResources().getColor(R.color.black));
			tv_guid2.setTextColor(getResources().getColor(R.color.black));
			tv_guid4.setTextColor(getResources().getColor(R.color.black_1a));
			tv_guid3.setTextColor(getResources().getColor(R.color.black));
			tv_guid5.setTextColor(getResources().getColor(R.color.black));
			tv_guid6.setTextColor(getResources().getColor(R.color.black));
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			// geta.finish();
			break;
		/*
		 * case R.id.message: Intent intent=new
		 * Intent(this,MessageActivity.class); startActivity(intent);
		 */

		default:
			break;
		}

	}
}
